
$(function() {
	$("input[id^='action_s']:visible").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: 'info.php?do=RolePriv.ajax_action',
				dataType: "json",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							label: item.title + " [" + item.cc + ", " + item.action + "]",
							value: item.action
						}
					}));
				}
			});
		},
		
		minLength: 1
	});

	$("#button_priv").click(function() {		
		post_find(this.form, "result_priv");
	});
});

var RolePriv = {

	popup: function (e, p2, id) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'info.php?do=RolePriv.edit&id=' + id;	
		var c2 = 'info.php?do=RolePriv.copy&id=' + id;

		var a1 = ['编辑信息', 'url', 'RolePriv.dialog_edit_', '550x300'];
		var a2 = ['复制信息', 'url', 'RolePriv.dialog_copy_', '550x300'];
		
		var s = c1 + ", " + c2;

		return popup(e, [a1, a2], s);
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"修改": function() {
				if (!confirm("确认是否修改？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = { };
				body.id = form.id.value;
				body.name = form.name.value;
				body.cc = form.cc.value;
				body.action_ = form.action_.value;
				body.status = form.status.value;
			
				$(this).dialog("close");

				var url = 'info.php?do=RolePriv.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},

	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"复制": function() {
				if (!confirm("确认是否复制？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.role = form.role.value;
				body.name = form.name.value;
				body.cc = form.cc.value;
				body.action_ = form.action_.value;
				body.status = form.status.value;
			
				$(this).dialog("close");

				var url = 'info.php?do=RolePriv.copyPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	}	
};